package cn.antblog.code;

import java.io.PrintStream;

/**
 * @ClassName CalculationBucket
 * @Description 计算hive 桶分区  分几个桶
 * @Author YangJingyi
 * @Date 2023/2/20 15:43
 * @Version 1.0
 */
public class CalculationBucket {
    public  final static PrintStream out = null;

    //dataVolPerPart 就是数据量 * 10000
    //DATA_VOL_PER_BUCKET 是 350 * 10000
    private static boolean isPrime(long src) {
        if (src < 2) {
            return false;
        }
        if (src == 2 || src == 3) {
            return true;
        }
        if (src % 2 == 0) {// 先判断是否为偶数，若偶数就直接结束程序
            return false;
        }

        double sqrt = Math.sqrt(src);
        for (int i = 3; i <= sqrt; i += 2) {
            if (src % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static int calculateBucketNum(long dataVolPerPart) {
        long DATA_VOL_PER_BUCKET = 350 * 10000;
        int bucketNum = (int) (dataVolPerPart / DATA_VOL_PER_BUCKET + 1);
        if (bucketNum < 2) return 1;
        if (bucketNum == 2) return 3;
        while (!isPrime(bucketNum) || bucketNum % 31 == 0) {
            bucketNum += 1;
        }
        return bucketNum;
    }

    public static String getRangePartitionStmt(String columnName, int startYear, int endYear) {
        StringBuilder stringBuilder = new StringBuilder(String.format("PARTITIONED BY RANGE (%s) (", columnName));
        stringBuilder.append(String.format("\n\tPARTITION partition_before%04d VALUES LESS THAN ('%04d-01-01')", startYear, startYear));
        for (int year = startYear; year <= endYear; year++) {
            for (int month = 1; month <= 12; month++) {
                int ltMonth = month == 12 ? 1 : month + 1;
                int ltYear = month == 12 ? year + 1 : year;
                stringBuilder.append(String.format("\n\t,PARTITION partition_%04d%02d VALUES LESS THAN ('%04d-%02d-01')", year, month, ltYear, ltMonth));
            }
        }
        stringBuilder.append("\n)");
        return stringBuilder.toString();
    }


    public static void basket(int data[])//data为待排序数组
    {
        int n=data.length;
        int bask[][]=new int[10][n];
        int index[]=new int[10];
        int max=Integer.MIN_VALUE;
        for(int i=0;i<n;i++)
        {
            max=max>(Integer.toString(data[i]).length())?max:(Integer.toString(data[i]).length());
        }
        String str;
        for(int i=max-1;i>=0;i--)
        {
            for(int j=0;j<n;j++)
            {
                str="";
                if(Integer.toString(data[j]).length()<max)
                {
                    for(int k=0;k<max-Integer.toString(data[j]).length();k++)
                        str+="0";
                }
                str+=Integer.toString(data[j]);
                bask[str.charAt(i)-'0'][index[str.charAt(i)-'0']++]=data[j];
            }
            int pos=0;
            for(int j=0;j<10;j++)
            {
                for(int k=0;k<index[j];k++)
                {
                    data[pos++]=bask[j][k];
                }
            }
            for(int x=0;x<10;x++)index[x]=0;
        }
    }

    public static void f(int a ) {
        if(a >= 1){
            System.out.println(a);
            f(--a);
        }
        System.out.println(a);
    }

    public static void main(String[] args) {
        //int i = calculateBucketNum(100 * 10000 * 22 );
        //System.out.println(i);
        //boolean i  = isPrime(101);
        //System.out.println(i);
        /*String col_1 = getRangePartitionStmt("COL_1", 2023, 2025);
        System.out.println(col_1);*/
        // f(5);

        StringBuilder sb = new StringBuilder("你好;nihao;");
        String s = sb.substring(0, sb.length()-1);
        System.out.println(s);
    }
}
